package com.example.bes019.single.ds.demo1.controller;

import com.example.bes019.single.ds.demo1.mapper.UserMapper;
import com.example.bes019.single.ds.demo1.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

@RestController
public class TestController {

    private static final Logger log = LoggerFactory.getLogger(TestController.class);

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired
    private DataSource dataSource;

    @GetMapping(value = "/test")
    public String test() throws SQLException {
        Connection connection = dataSource.getConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from t_user");
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            System.out.println("ID: " + id);
        }
        return "SUCCESS";
    }

    /**
     * 插叙所有用户数据
     * @return
     */
    @GetMapping(value = "/user/all")
    public List<User> selectAll() {
        return userMapper.selectUserList();
    }

    @GetMapping(value = "/slow")
    public void slow() {
        userMapper.slowsql();
    }

}
